<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.rule_id ? $t('编辑') : $t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="520px" top="5vh" @close='close'>
    <div class="dialogContentBoxMaxHeight90">
      <el-form ref="form" :inline="true" :model="record" label-width="150px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('协议名称')" class="bacnetForm" prop="proto">
          <span style="color:#999;">{{ rule[record.proto] }}</span>
        </el-form-item>
        <el-form-item :label="$t('动作')" class="bacnetForm" prop="action">
          <el-select class="bacnetLength" ref="action" v-model="record.action" size='small' filterable>
            <el-option v-for="(value, key) in actionList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('功能码')" class="bacnetForm" prop="func">
          <el-select class="bacnetLength" v-model="record.func" ref="func" size='small' filterable>
            <el-option v-for="(value, key) in bacnetFuncList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('对象标识类型')" class="bacnetForm" prop="object_type" key="object_type">
          <el-input v-emoji v-model="record.object_type" ref="objectType" class="bacnetLength" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('对象标识符起始地址')" class="bacnetForm" prop="object_id_start" key="object_id_start">
          <el-input v-emoji v-model="record.object_id_start" ref="objectIdStart" class="bacnetLength" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('对象标识符结束地址')" class="bacnetForm" prop="object_id_stop" key="object_id_stop">
          <el-input v-emoji v-model="record.object_id_stop" ref="objectIdStop" class="bacnetLength" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('属性标识符起始地址')" class="bacnetForm" prop="property_id_start" key="property_id_start">
          <el-input v-emoji v-model="record.property_id_start" ref="propertyIdStart" class="bacnetLength" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('属性标识符结束地址')" class="bacnetForm" prop="property_id_stop" key="property_id_stop">
          <el-input v-emoji v-model="record.property_id_stop" ref="propertyIdStop" class="bacnetLength" size='small'></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>

  </el-dialog>
</template>
<script>
import ruleArray from '../../ruleArray'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'BacnetDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else {
        callback()
      }
    }
    var validateObjectStart = (rule, value, callback) => {
      let flag = this.record.object_id_stop
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (this.record.object_id_start && this.record.object_id_stop && (Number(this.record.object_id_start) > Number(this.record.object_id_stop))) {
        callback(new Error(this.$t('起始地址必须小于结束地址')))
      } else {
        if (!isNaN(Number(flag)) && this.isInteger(flag) && (Number(flag) >= 0 && Number(flag) <= 65535)) {
          this.$refs['form'].clearValidate(['object_id_stop'])
        }
        callback()
      }
    }
    var validateObjectStop = (rule, value, callback) => {
      let flag = this.record.object_id_start
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (this.record.object_id_start && this.record.object_id_stop && (Number(this.record.object_id_start) > Number(this.record.object_id_stop))) {
        callback(new Error(this.$t('起始地址必须小于结束地址')))
      } else {
        if (!isNaN(Number(flag)) && this.isInteger(flag) && (Number(flag) >= 0 && Number(flag) <= 65535)) {
          this.$refs['form'].clearValidate(['object_id_start'])
        }
        callback()
      }
    }
    var validatePropertyStart = (rule, value, callback) => {
      let flag = this.record.property_id_stop
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (this.record.property_id_start && this.record.property_id_stop && (Number(this.record.property_id_start) > Number(this.record.property_id_stop))) {
        callback(new Error(this.$t('起始地址必须小于结束地址')))
      } else {
        if (!isNaN(Number(flag)) && this.isInteger(flag) && (Number(flag) >= 0 && Number(flag) <= 65535)) {
          this.$refs['form'].clearValidate(['property_id_stop'])
        }
        callback()
      }
    }
    var validatePropertyStop = (rule, value, callback) => {
      let flag = this.record.property_id_start
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('只能输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 65535) {
        callback(new Error(this.$t('输入范围0~65535')))
      } else if (this.record.property_id_start && this.record.property_id_stop && (Number(this.record.property_id_start) > Number(this.record.property_id_stop))) {
        callback(new Error(this.$t('起始地址必须小于结束地址')))
      } else {
        if (!isNaN(Number(flag)) && this.isInteger(flag) && (Number(flag) >= 0 && Number(flag) <= 65535)) {
          this.$refs['form'].clearValidate(['property_id_start'])
        }
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        action: 'accept',
        func: 'read',
        object_id_start: '',
        object_id_stop: '',
        object_type: '',
        property_id_start: '',
        property_id_stop: '',
        proto: 'bacnet',
        rule_id: null
      },
      recordCopy: {
        action: 'accept',
        func: 'read',
        object_id_start: '',
        object_id_stop: '',
        object_type: '',
        property_id_start: '',
        property_id_stop: '',
        proto: 'bacnet',
        rule_id: null
      },
      rule: ruleArray.rule,
      actionList: { 'accept': '允许', 'alert': '告警' },
      bacnetFuncList: ruleArray.bacnetFuncList,
      rules: {
        object_type: [{ required: false, validator: validateName, trigger: 'blur' }],
        object_id_start: [{ required: false, validator: validateObjectStart, trigger: 'blur' }],
        object_id_stop: [{ required: false, validator: validateObjectStop, trigger: 'blur' }],
        property_id_start: [{ required: false, validator: validatePropertyStart, trigger: 'blur' }],
        property_id_stop: [{ required: false, validator: validatePropertyStop, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['objectType', 'objectIdStart', 'objectIdStop', 'propertyIdStop', 'propertyIdStart']
        inputClass(this.$refs, data)
        const selectData = ['action', 'func']
        selectClass(this.$refs, selectData)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          var formData = JSON.parse(JSON.stringify(this.record))
          this.$emit('addConfigRuleMap', formData)
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.$emit('childrenClose')
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
<style>
.bacnetLength {
  width: 250px;
}
</style>
